﻿using System.Collections.Generic;
using System.Net;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Services.Helpers;
using Nop.Services.WindAlert;
using Nop.Web.Models.WindAlert;

namespace Nop.Web.Controllers
{
    public partial class WMetarController : BasePublicController
    {
        #region Fields
        private readonly IWorkContext _workContext;
        private readonly IWindHelpers _windHelpers;
        #endregion

        #region Constructors

        public WMetarController(IWorkContext workContext, IWindHelpers windHelpers)
        {
            this._workContext = workContext;
            this._windHelpers = windHelpers;
        }

        #endregion
        
        #region Utitilies

        [NonAction]
        private List<SelectListItem> PrepareAvailableMetars(string selectedValue)
        {
            var availableMetars = new List<SelectListItem>();
            string[] availableMetarsStrings = new string[] 
            {
                "most recent only","past 1 hours","past 2 hours","past 3 hours","past 4 hours","past 5 hours","past 6 hours","past 12 hours","past 18 hours","past 24 hours","past 36 hours"
            };
            for (int i = 0; i < availableMetarsStrings.Length; i++)
            {
                availableMetars.Add(new SelectListItem() { Text = availableMetarsStrings[i], Value = availableMetarsStrings[i], Selected = (availableMetarsStrings[i] == selectedValue)});
            }
            return availableMetars;
        }

        [NonAction]
        protected bool IsCurrentUserRegistered()
        {
            return _workContext.CurrentCustomer.IsRegistered();
        }

         [NonAction]
         public string GetMetar(string airport, string taf)
         {
             //http://www.aviationweather.gov/adds/metars/?station_ids=llbg&std_trans=standard&chk_metars=on&hoursStr=past+1+hours&submitmet=Submit
             //http://www.aviationweather.gov/adds/metars/?station_ids=llbg&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&chk_tafs=on&submitmet=Submit
             string strUrl = "http://www.aviationweather.gov/adds/metars/?station_ids=" + airport;
             strUrl += "&std_trans=translated&chk_metars=on&hoursStr=" + taf;
             strUrl += "&chk_tafs=on&submitmet=Submit";
             string results = _windHelpers.GetPageData(strUrl);
             if (results.IndexOf("Sorry") != -1)
                 return "Please enter a valid airport code !!!";
             return results;
            
         }

        #endregion

        public ActionResult Index()
        {
            var model = new MetarModel();

            if (!IsCurrentUserRegistered())
                model.IsRegistered = false;
            else
                model.IsRegistered = true;

            model.AvailableMetars = PrepareAvailableMetars("Most recent only");
           
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(MetarModel model)
        {
            model.AvailableMetars = PrepareAvailableMetars(model.MetarListId );
            model.MetarTafResults = GetMetar(model.Airport, model.MetarListId); 
            return View(model);
        }
    }
}
